<?php
/**
 * 
 * @App	
 * @Group 
 * @Action SwooleWebSocketServerAction
 * @author lilei
 * @date 2018年11月22日
 * 
 *
 */
namespace app\crontab\controller\swoole;
use app\crontab\controller\CrontabCommon;
use swoole_websocket_server;

class Websocket extends CrontabCommon {
	function test() {
		$server = new swoole_websocket_server ( "0.0.0.0", 9502 );
		
		$server->on ( 'open', function ($server, $req) {
			echo "connection open: {$req->fd}\n";
		} );
		
		$server->on ( 'message', function ( \swoole_websocket_server $server, $frame) {
			echo "received message: {$frame->data}\n";
			$server->push ( $frame->fd, json_encode ( [ 
					"hello",
					"world" 
			] ) );
		} );
		
		$server->on ( 'close', function ($server, $fd) {
			echo "connection close: {$fd}\n";
		} );
		
		$server->start ();
	}
}